﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SportManager
{
    public partial class AddExerciseDlg : Form
    {
        private ExerciseType m_type;
        private bool IsEditMode = false;
        public ExerciseInfo ResultInfo;
        
        public AddExerciseDlg(ExerciseType type)
        {
            InitializeComponent();
            dialogHeader1.SetHeaderText("Добавление нового упражнения");
            dialogHeader1.SetDescriptionText("Задайте необходимые параметры и нажмите ОК");
            m_type = type;
        }

        public AddExerciseDlg(ExerciseInfo info)
        {
            IsEditMode = true;
            InitializeComponent();
            this.Text = "Редактирование упражнения";
            dialogHeader1.SetHeaderText("Редактирование существующего упражнения");
            dialogHeader1.SetDescriptionText("Задайте необходимые параметры и нажмите ОК");
            dialogHeader1.SetIcon(Resource.dumbbells_icon32);

            checkBoxApproachs.Checked = info.IsApproachRequiered;
            checkBoxApproachRest.Checked = info.IsApproachsRestRequired;
            checkBoxDistance.Checked = info.IsDistanceRequired;
            checkBoxIntesity.Checked = info.IsIntensityRequired;
            checkBoxRepeats.Checked = info.IsRepeatsRequired;
            checkBoxRepeatsRest.Checked = info.IsRepeatsRestRequiered;
            checkBoxTempo.Checked = info.IsTempoRequired;
            checkBoxTimeSpan.Checked = info.IsTimeSpanRequired;            
            checkBoxWeight.Checked = info.IsWeightRequired;

            ResultInfo = info;
        }

        private void AddExerciseDlg_Load(object sender, EventArgs e)
        {
            foreach (ExerciseType iter in Enum.GetValues(typeof(ExerciseType)))
            {
                comboBoxTypes.Items.Add(iter.EnumToString());
            }

            foreach (ExerciseLoad iter in Enum.GetValues(typeof(ExerciseLoad)))
            {
                if (iter != ExerciseLoad.None)
                {
                    listBox1.Items.Add(iter.EnumToString());
                }
            }

            if (ResultInfo != null)
            {
                textBox1.Text = ResultInfo.Name;
                comboBoxTypes.SelectedIndex = (int)ResultInfo.Type;

                ListBox.SelectedIndexCollection selectedLoads = new ListBox.SelectedIndexCollection(listBox1);
                foreach (ExerciseLoad iter in ResultInfo.Load)
                {
                    if (iter != ExerciseLoad.None)
                    {
                        selectedLoads.Add((int)iter - 1);
                    }
                }

                richTextBox1.Text = ResultInfo.Description;
            }
            else
            {
                comboBoxTypes.SelectedIndex = (int)m_type;
                listBox1.SelectedIndex = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!IsEditMode)
            {
                ResultInfo = new ExerciseInfo(textBox1.Text, richTextBox1.Text, (ExerciseType)comboBoxTypes.SelectedIndex
                    , GetLoadsSelectionList()
                );                
            }
            else
            { 
                ResultInfo.Name = textBox1.Text;
                ResultInfo.Description = richTextBox1.Text;
                ResultInfo.Type = (ExerciseType)comboBoxTypes.SelectedIndex;
                ResultInfo.Load = GetLoadsSelectionList();
            }

            ResultInfo.IsApproachRequiered = checkBoxApproachs.Checked;
            ResultInfo.IsApproachsRestRequired = checkBoxApproachRest.Checked;
            ResultInfo.IsDistanceRequired = checkBoxDistance.Checked;
            ResultInfo.IsIntensityRequired = checkBoxIntesity.Checked;
            ResultInfo.IsRepeatsRequired = checkBoxRepeats.Checked;
            ResultInfo.IsRepeatsRestRequiered = checkBoxRepeatsRest.Checked;
            ResultInfo.IsTempoRequired = checkBoxTempo.Checked;
            ResultInfo.IsTimeSpanRequired = checkBoxTimeSpan.Checked;
            ResultInfo.IsUserDefined = true;
            ResultInfo.IsWeightRequired = checkBoxWeight.Checked;
        }

        private List<ExerciseLoad> GetLoadsSelectionList()
        {
            List<ExerciseLoad> loads = new List<ExerciseLoad>();
            foreach (int item in listBox1.SelectedIndices)
            {
                if ((item + 1) != (int)ExerciseLoad.None)
                {
                    loads.Add((ExerciseLoad)(item + 1));
                }
            }

            if (loads.Count == 0)
            {
                loads.Add(ExerciseLoad.None);
            }

            return loads;
        }
    }
}
